<template>
  <div id="app">
    <nav>
      <router-link to="/">首页</router-link> |
      <router-link to="/about">关于</router-link> |
      <router-link to="/fruit/orange">橙子</router-link> |
      <router-link to="/fruit/banana">香蕉</router-link> |
      <router-link to="/fruit/apple">苹果</router-link> |
      <router-link to="/admin">管理</router-link>
      <!-- 动态控制登录链接的显示 -->
      <router-link v-if="!isLoggedIn" to="/login"> | 登录</router-link>
    </nav>
    <router-view/>
  </div>
</template>

<script>
export default {
  name: 'App',
  data() {
    return {
      // 添加一个本地状态来存储登录状态
      isAuthenticated: localStorage.getItem('isAuthenticated') === 'true'
    };
  },
  computed: {
    // 判断用户是否已登录
    isLoggedIn() {
      return this.isAuthenticated;
    }
  },
  created() {
    // 监听 localStorage 的变化
    window.addEventListener('storage', this.handleStorageChange);
  },
  beforeDestroy() {
    // 移除事件监听器
    window.removeEventListener('storage', this.handleStorageChange);
  },
  methods: {
    handleStorageChange(event) {
      if (event.key === 'isAuthenticated') {
        this.isAuthenticated = event.newValue === 'true';
      }
    }
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

nav {
  padding: 30px;
}

nav a {
  font-weight: bold;
  color: #2c3e50;
}

nav a.router-link-exact-active {
  color: #42b983;
}
</style>
